﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Steamrunner
{
    class Highscore
    {
        string filename;

        IniFile highscoreFile;

        Dictionary<string, int> highscoreDict = new Dictionary<string, int>();

        public Highscore(string filename)
        {
            this.filename = filename;
        }

        public void Initialize()
        {
            highscoreFile = new IniFile(filename);
            highscoreFile.Parse();

            highscoreDict.Clear();
            Dictionary<string, Dictionary<string, float>> highscoreList = highscoreFile.Options;
            foreach (KeyValuePair<string, Dictionary<string, float>> group in highscoreList)
            {
                foreach (KeyValuePair<string, float> pair in group.Value)
                {
                    highscoreDict[pair.Key] = (int)pair.Value;
                }
            }
        }

        public void AddScore(string name, int time)
        {
            if (highscoreDict.ContainsKey(name))
            {
                name += "1";
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                if (highscoreDict.ContainsKey(name))
                {
                    name += "1";
                }
                //highscoreDict.Add(name, time);
                Sort(name, time);
                Save();
            }
            else
            {
                //highscoreDict[name] = time;
                Sort(name, time);
                Save();
            }
        }

        private void Sort(string name, int time)
        {
            Dictionary<string, int> tempHighscoreDict = new Dictionary<string, int>();
            foreach (KeyValuePair<string, int> pair in highscoreDict)
            {
                tempHighscoreDict.Add(pair.Key, pair.Value);
            }
            tempHighscoreDict.Add(name, time);

            Dictionary<string, int> sortedList = new Dictionary<string, int>();

            while (sortedList.Count < 10)
            {
                int bestTime = 1000000;
                string bestName = "";
                foreach (KeyValuePair<string, int> pair in tempHighscoreDict)
                {
                    if (pair.Value < bestTime)
                    {
                        bestTime = pair.Value;
                        bestName = pair.Key;
                    }
                }
                sortedList[bestName] = bestTime;
                tempHighscoreDict.Remove(bestName);
            }

            highscoreDict = sortedList;
        }

        private void Save()
        {
            Dictionary<string, Dictionary<string, float>> highscoreList = new Dictionary<string, Dictionary<string, float>>();
            highscoreList["Highscore"] = new Dictionary<string, float>();

            foreach (KeyValuePair<string, int> pair in highscoreDict)
            {
                highscoreList["Highscore"][pair.Key] = pair.Value;
            }

            highscoreFile.Options = highscoreList;
            highscoreFile.Save();
        }

        public bool IsScoreGoodEnough(int score)
        {
            foreach (KeyValuePair<string, int> pair in highscoreDict)
            {
                if (score < pair.Value)
                {
                    return true;
                }
            }
            return false;
        }

        public string Name(int index)
        {
            int i = 0;
            foreach (KeyValuePair<string, int> pair in highscoreDict)
            {
                if (i == index)
                {
                    return pair.Key;
                }
                i++;
            }
            return "";
        }

        public int Score(string name)
        {
            return highscoreDict[name];
        }
    }
}
